import 'package:flutter/material.dart';
import 'package:woandroid/pages/BaseState.dart';
import 'package:woandroid/http/HttpUtilWithCookie.dart';
import 'package:woandroid/http/Api.dart';
import 'package:woandroid/pages/ArticleDetailPage.dart';
import 'package:woandroid/util/FlutterUtil.dart';
import 'package:woandroid/pages/SearchPage.dart';

//  热搜page
class HotSearchPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new HotSearchPageState();
  }
}
class HotSearchPageState extends BaseState<HotSearchPage>{

  List<Widget> hotKeyWidgets=new List();
  List<Widget> friendWidgets=new List();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _getHotKey();
    _getFriend();
  }

  @override
  Widget build(BuildContext context) {

    return new ListView(
      children: <Widget>[
        Padding(child:
        Text('热搜',style: TextStyle(color: Colors.indigoAccent),),
          padding: EdgeInsets.all(10),
        ),

        Padding(
          padding: EdgeInsets.only(left: 10,right: 10),
          child: Wrap(spacing: 5,runSpacing: 5,children: hotKeyWidgets,),
        ),

        Padding(child:
        Text('常用网站',style: TextStyle(color: Colors.indigoAccent),),
          padding: EdgeInsets.all(10),
        ),

        Padding(
          padding: EdgeInsets.only(left: 10,right: 10),
          child: Wrap(spacing: 5,runSpacing: 5,children: friendWidgets,),
        ),


      ],
    );

  }

  void _getHotKey() {
    HttpUtil.get(Api.HOTKEY, (data) {
      setState(() {
        List datas = data;
        hotKeyWidgets.clear();
        for (var value in datas) {
          Widget actionChip = new ActionChip(
              backgroundColor: Theme.of(context).accentColor,
              label: new Text(
                value['name'],
                style: new TextStyle(color: Colors.white),
              ),
              onPressed: () {

//                Navigator
//                    .of(context)
//                    .pushReplacement(new MaterialPageRoute(builder: (context) {
//                  return new SearchPage(value['name']);
//                })
//                );
                FlutterUtil.pushReplacement(context, new SearchPage(value['name'])) ;

              });

          hotKeyWidgets.add(actionChip);
        }
      });
    });


  }

  void _getFriend() {
    HttpUtil.get(Api.FRIEND, (data){

      setState(() {
        List ds=data;
        friendWidgets.clear();
        for(var itemData in ds){
          Widget actionChip = new ActionChip(
              backgroundColor: Theme.of(context).accentColor,
              label: new Text(
                itemData['name'],
                style: new TextStyle(color: Colors.white),
              ),
              onPressed: () {
               FlutterUtil.pushReplacement(context, new ArticleDetailPage(
                   title: itemData['name'], url: itemData['link'])) ;
              });

          friendWidgets.add(actionChip);
        }
      });
    });
  }
}